package com.hb.android.autoreboot;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.Date;
import java.util.Locale;

public class Utils {

    /**
     * 日志总开关
     */
    public static boolean isDebug = true;
    /**
     * 把日志输出到缓存文件
     */
    public static boolean isOutputLog = false;

    /**
     * 日志标记数目
     */
    private static int index = 0;

    /**
     * 计算时间
     *
     * @param seconds 秒数
     * @return 转换后的时间
     */
    public static String calculatePlayTime(int seconds) {
        int minute = seconds / 60;
        int second = seconds % 60;
        int hour = minute / 60;
        minute = minute % 60;
        if (hour > 0) {
            return String.format(Locale.getDefault(), "%02d:%02d:%02d", hour, minute, second);
        }
        return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
    }

    public static void logE(Context context, String t, String m) {
        if (isDebug) {
            Log.e(t, m);
        }
        if (isOutputLog) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("mLog", Context.MODE_PRIVATE);
            if (sharedPreferences == null) {
                return;
            }
            index = sharedPreferences.getInt("index", 0);
            index++;
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(String.valueOf(index), "e：" + t + "：" + new Date() + m);
            editor.putInt("index", index);
            editor.apply();
        }
    }

    public static void logD(Context context, String t, String m) {
        if (isDebug) {
            Log.d(t, m);
        }
        if (isOutputLog) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("mLog", Context.MODE_PRIVATE);
            if (sharedPreferences == null) {
                return;
            }
            index = sharedPreferences.getInt("index", 0);
            index++;
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(String.valueOf(index), "d：" + t + "：" + new Date() + m);
            editor.putInt("index", index);
            editor.apply();
        }
    }

    public static void logW(Context context, String t, String m) {
        if (isDebug) {
            Log.w(t, m);
        }
        if (isOutputLog) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("mLog", Context.MODE_PRIVATE);
            if (sharedPreferences == null) {
                return;
            }
            index = sharedPreferences.getInt("index", 0);
            index++;
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(String.valueOf(index), "w：" + t + "：" + new Date() + m);
            editor.putInt("index", index);
            editor.apply();
        }
    }

}
